package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.PageDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐管理相关接口")
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @ApiOperation("套餐分页搜索")
    @GetMapping("/page")
    public Result<PageDTO<SetmealDTO>> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页搜索：{}", setmealPageQueryDTO);
        PageDTO<SetmealDTO> pageDTO = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageDTO);
    }

    @ApiOperation("新建套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新建套餐:{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐:{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("根据套餐id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealDTO> getBySetmealId(@PathVariable long id){
        log.info("根据套餐id查询套餐:{}", id);
        SetmealDTO setmealDTO = setmealService.getBySetmealId(id);
        return Result.success(setmealDTO);
    }


    @ApiOperation("起售停售套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result StartOrStop(@PathVariable Integer status, long id){
        log.info("修改id{}：的套餐状态为:{}", id, status == StatusConstant.ENABLE ? "起售" : "停售");
        setmealService.StratOrStop(status, id);
        return Result.success();
    }


    @ApiOperation("根据套餐id批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除套餐:{}", ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

}
